/*
 * @Author: xiaoyan
 * @Date: 2021-11-24 14:05:32
 * @LastEditors: xiaoyan
 * @LastEditTime: 2022-01-19 09:44:44
 * @Description: 
 * @FilePath: \user_auth_front\src\pages\organization\components\add.tsx
 */
import { Input, Form, message } from "antd";
import { useDispatch} from 'redux-react-hook';
import { closeModal } from "@/store/modules/app"
import store from "@/store";
import { organizationAdd,organizationUpdate,organizationDetail} from "@/api";
import { useEffect, useMemo} from "react";
export default ()=>{
    const dispatch = useDispatch();
    const [form] = Form.useForm();
    const dialogData:any = store.getState().app.modalConf;
    const id = useMemo(()=>dialogData.data,[dialogData.data]);
    dialogData.handleOk = form.submit;//设置手动提交事件
    useEffect(()=>{
        if(id) ajaxDetail(id);
    },[id]);
    //请求详情数据
    const ajaxDetail = (id:Number)=>{
        organizationDetail({id}).then((res:any)=>{
            if(res.code === 0){
                form.setFieldsValue({
                    name:res.data.name
                });
            }else{
                message.error(res.msg);
            }
        });
    };
    const onFinish = (data:Object)=>{
        handleSubmit(data).then((res:any)=>{
            if(res.code === 0){
                const modalConf:any = store.getState().app.modalConf;
                if(typeof modalConf.handleBack === "function") {
                    id?modalConf.handleBack() : modalConf.handleBack({page:1});
                }
                dispatch(closeModal());
            }else{
                message.error(res.msg);
            }
        });
    };
    const handleSubmit = (data:Object)=>{
        if(id){
            return organizationUpdate({organizationId:id,...data});
        }else{
            return organizationAdd(data);
        }
    };
    return(
        <Form form={form} onFinish={onFinish}>
            <Form.Item label="机构名称：" name="name" rules={[{required: true, message: '请输入机构名称'}]}>
                <Input allowClear style={{width:200}} />
            </Form.Item>
        </Form>
    );
}